package com.jz.community.modulemine.rechargephone.model;

import android.content.Context;

import com.jz.community.basecomm.net.retrofit.ApiException;
import com.jz.community.basecomm.net.retrofit.ApiUtils;
import com.jz.community.basecomm.net.retrofit.HttpRxObservable;
import com.jz.community.basecomm.net.retrofit.JsonRequestBodyConverter;
import com.jz.community.basecomm.net.retrofit.RxLoadingWrapper;
import com.jz.community.basecomm.net.rxandroid.OnLoadListener;
import com.jz.community.basecomm.utils.LoggerUtils;
import com.jz.community.modulemine.money.bean.MoneyInfoBean;
import com.jz.community.modulemine.money.bean.MoneyRechargeBean;
import com.jz.community.modulemine.rechargephone.bean.FareDealBean;
import com.jz.community.modulemine.rechargephone.bean.PhoneFareBean;
import com.jz.community.modulemine.rechargephone.bean.PhonePriceBean;
import com.jz.community.modulemine.rechargephone.net.PhoneNetApi;
import com.orhanobut.logger.Logger;
import com.trello.rxlifecycle2.components.support.RxFragmentActivity;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observable;

public class PhoneModelImp implements PhoneModel {

    private Context mContext;

    public PhoneModelImp(Context mContext) {
        this.mContext = mContext;
    }

    @Override
    public void initPhoneData(int page, int size, OnLoadListener<PhonePriceBean> onLoadListener) {
        Observable<PhonePriceBean> observer = HttpRxObservable.getObservable((RxFragmentActivity) mContext, ApiUtils.getApi(mContext, PhoneNetApi.class)
                .initPhoneData(page, size));
        new RxLoadingWrapper<PhonePriceBean>(mContext, false)
                .execute(observer)
                .subscribe(resultBean -> {
                    if (resultBean == null) {
                        return;
                    }
                    onLoadListener.onSuccess(resultBean);
                    LoggerUtils.fLog().i("ceshi " + resultBean.toString());
                }, throwable -> onLoadListener.onFail(throwable.toString(), ApiException.handleException(throwable).code));

    }

    @Override
    public void initMyMoneyData(OnLoadListener<MoneyInfoBean> onLoadListener) {
        Observable<MoneyInfoBean> observer = HttpRxObservable.getObservable((RxFragmentActivity) mContext, ApiUtils.getApi(mContext, PhoneNetApi.class)
                .initMoneyData());
        new RxLoadingWrapper<MoneyInfoBean>(mContext, false)
                .execute(observer)
                .subscribe(resultBean -> {
                    if (resultBean == null) {
                        return;
                    }
                    onLoadListener.onSuccess(resultBean);
                    LoggerUtils.fLog().i("ceshi " + resultBean.toString());
                }, throwable -> onLoadListener.onFail(throwable.toString(), ApiException.handleException(throwable).code));
    }

    @Override
    public void rechargePhone(String phone, int type, String id, OnLoadListener<PhoneFareBean> onLoadListener) {
        String payType = "";
        if (type == 1) {
            //微信
            payType = "MicroMessenger";
        } else if (type == 2) {
            //支付宝
            payType = "Alipay";
        } else if (type == 4) {
            //社区人E卡
            payType = "Ecard";
        }
        Map<String, String> params = new HashMap<>();
        params.put("mark", "101");
        params.put("payType", payType);
        params.put("phoneChargeId", id);
        params.put("phoneNum", phone);

        Observable<PhoneFareBean> observer = HttpRxObservable.getObservable((RxFragmentActivity) mContext, ApiUtils.getApi(mContext, PhoneNetApi.class)
                .rechargePhone(new JsonRequestBodyConverter<Map<String, String>>().convert(params)));
        new RxLoadingWrapper<PhoneFareBean>(mContext, false)
                .execute(observer)
                .subscribe(resultBean -> {
                    if (resultBean == null) {
                        return;
                    }
                    onLoadListener.onSuccess(resultBean);
                    LoggerUtils.fLog().i("ceshi " + resultBean.toString());
                }, throwable -> onLoadListener.onFail(throwable.toString(), ApiException.handleException(throwable).code));
    }

    @Override
    public void initPhoneFare(int page, int size, OnLoadListener<FareDealBean> onLoadListener) {
        Observable<FareDealBean> observer = HttpRxObservable.getObservable((RxFragmentActivity) mContext, ApiUtils.getApi(mContext, PhoneNetApi.class)
                .initPhoneFare(page, size, 0));
        new RxLoadingWrapper<FareDealBean>(mContext, false)
                .execute(observer)
                .subscribe(resultBean -> {
                    if (resultBean == null) {
                        return;
                    }
                    onLoadListener.onSuccess(resultBean);
                    LoggerUtils.fLog().i("ceshi " + resultBean.toString());
                }, throwable -> onLoadListener.onFail(throwable.toString(), ApiException.handleException(throwable).code));

    }
}
